﻿using System;
using Castle.Core.Interceptor;

namespace Fadd.Remoting
{
	internal class InterfaceProxy : IInterceptor
	{
		private readonly RemotingClient _client;
		private readonly Type _proxiedService;

		public InterfaceProxy(RemotingClient client, Type proxiedService)
		{
			_client = client;
			_proxiedService = proxiedService;
		}

		#region IInterceptor Members

		public void Intercept(IInvocation invocation)
		{
			invocation.ReturnValue = invocation.Method.IsGenericMethod
			                         	? _client.InvokeGeneric(_proxiedService, invocation.Method.Name,
			                         	                        invocation.GenericArguments, invocation.Arguments)
			                         	: _client.Invoke(_proxiedService, invocation.Method.Name, invocation.Arguments);
			invocation.Proceed();
		}

		#endregion
	}
}